package com.atguigu.web;

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();//本文件是web层,通过serice层,调用dao层,控制数据库
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println(req.toString());//打印不出来,如何打印req内部信息
        String username=req.getParameter("username");
        String password=req.getParameter("password");

        User loginUser=userService.login(new User(null,username,password,null));
        if(loginUser==null){
            //System.out.println("写的什么记吧玩意,不对,重写");
            req.setAttribute("msg","写的什么记吧玩意,不对,重写");
            req.setAttribute("username",username);
            req.setAttribute("password",password);
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);//请求转发
        }else{
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
        }
    }
}
